using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using System.Collections;

namespace OgilvyOne.SqlServerDAL
{
    public class User : IUser
    {
        // Fields
        private const string PARM_ISDEL = "@IsDel";
        private const string PARM_USER_CREATE_DATE = "@User_Create_Date";
        private const string PARM_USER_LOGON_CODE = "@User_Logon_Code";
        private const string PARM_USER_NAME = "@User_Name";
        private const string PARM_USER_PASSWORD = "@User_Password";
        private const string PARM_USER_STATUS = "@User_Status";
        private const string PARM_USER_UID = "@User_UID";
        private const string PARM_USER_UPDATE_DATE = "@User_Update_Date";
        private const string SQL_DELETE_USER = "UPDATE Sys_User SET IsDel = '1' WHERE User_UID = @User_UID";
        private const string SQL_INSERT_USER = "INSERT INTO Sys_User VALUES(@User_Name,@User_Logon_Code,@User_Password,@User_Status,getDate(),null,'0')";
        private const string SQL_SELECT_USER = "SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status,User_Create_Date,User_Update_Date FROM Sys_User  where User_UID=@User_UID and IsDel='0'";
        private const string SQL_SELECT_USER_LIST = "SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status,User_Create_Date,User_Update_Date FROM Sys_User where IsDel='0'";
        private const string SQL_SELECT_USER_NAMEPWD = "SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status FROM Sys_User where IsDel='0' and User_Logon_Code=@User_Logon_Code and User_Password=@User_Password and User_Status='1'";
        private const string SQL_SELECT_USER_SEARCHCODELIST = "SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status,User_Create_Date,User_Update_Date FROM Sys_User where IsDel='0' and User_Logon_Code like '%{0}%'";
        private const string SQL_SELECT_USER_SEARCHLIST = "SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status,User_Create_Date,User_Update_Date FROM Sys_User where IsDel='0' and User_Logon_Code like '%{0}%' and User_Name like '%{1}%'";
        private const string SQL_SELECT_USER_SEARCHNAMELIST = "SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status,User_Create_Date,User_Update_Date FROM Sys_User where IsDel='0' and User_Name like '%{0}%'";
        private const string SQL_SELECT_USER_STATUSLIST = "SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status,User_Create_Date,User_Update_Date FROM Sys_User where IsDel='0' and User_Status=@User_Status";
        private const string SQL_UPDATE_USER = "UPDATE Sys_User SET User_Name = @User_Name, User_Logon_Code = @User_Logon_Code, User_Password = @User_Password, User_Status = @User_Status, User_Update_Date = getDate() WHERE User_UID = @User_UID";

        // Methods
        public void Delete(int user_id)
        {
            SqlParameter[] userDeleteParameters = GetUserDeleteParameters();
            userDeleteParameters[0].Value = user_id;
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "UPDATE Sys_User SET IsDel = '1' WHERE User_UID = @User_UID", userDeleteParameters);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }

        private static SqlParameter[] GetUserDeleteParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("UPDATE Sys_User SET IsDel = '1' WHERE User_UID = @User_UID");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@User_UID", SqlDbType.Int, 4) };
                SQLHelper.CacheParameters("UPDATE Sys_User SET IsDel = '1' WHERE User_UID = @User_UID", cachedParameters);
            }
            return cachedParameters;
        }

        public int getUserID(string login_name, string user_password)
        {
            int num = 0;
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@User_Logon_Code", SqlDbType.VarChar, 0x26, "User_Logon_Code"), new SqlParameter("@User_Password", SqlDbType.VarChar, 0x80, "USER_Password") };
            cmdParms[0].Value = login_name;
            cmdParms[1].Value = user_password;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status FROM Sys_User where IsDel='0' and User_Logon_Code=@User_Logon_Code and User_Password=@User_Password and User_Status='1'", cmdParms))
            {
                while (reader.Read())
                {
                    num = reader.GetInt32(0);
                }
            }
            return num;
        }

        public UserInfo getUserInfo(string login_name, string user_password)
        {
            UserInfo info = null;
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@User_Logon_Code", SqlDbType.VarChar, 0x26, "User_Logon_Code"), new SqlParameter("@User_Password", SqlDbType.VarChar, 0x80, "USER_Password") };
            cmdParms[0].Value = login_name;
            cmdParms[1].Value = user_password;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status FROM Sys_User where IsDel='0' and User_Logon_Code=@User_Logon_Code and User_Password=@User_Password and User_Status='1'", cmdParms))
            {
                while (reader.Read())
                {
                    info = new UserInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4));
                }
            }
            return info;
        }

        private static SqlParameter[] GetUserInsertParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("INSERT INTO Sys_User VALUES(@User_Name,@User_Logon_Code,@User_Password,@User_Status,getDate(),null,'0')");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@User_Name", SqlDbType.VarChar, 0x20), new SqlParameter("@User_Logon_Code", SqlDbType.VarChar, 0x26), new SqlParameter("@User_Password", SqlDbType.VarChar, 0x80), new SqlParameter("@User_Status", SqlDbType.Char, 1) };
                SQLHelper.CacheParameters("INSERT INTO Sys_User VALUES(@User_Name,@User_Logon_Code,@User_Password,@User_Status,getDate(),null,'0')", cachedParameters);
            }
            return cachedParameters;
        }

        public IList getUserList()
        {
            IList list = new ArrayList();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status,User_Create_Date,User_Update_Date FROM Sys_User where IsDel='0'", null))
            {
                while (reader.Read())
                {
                    UserInfo info = new UserInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4));
                    list.Add(info);
                }
            }
            return list;
        }

        public IList getUserList(int userid)
        {
            IList list = new ArrayList();
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@User_UID", SqlDbType.Int, 4, "user_uid") };
            cmdParms[0].Value = userid;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status,User_Create_Date,User_Update_Date FROM Sys_User  where User_UID=@User_UID and IsDel='0'", cmdParms))
            {
                while (reader.Read())
                {
                    UserInfo info = new UserInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4));
                    list.Add(info);
                }
            }
            return list;
        }

        public IList getUserList(string status)
        {
            IList list = new ArrayList();
            SqlParameter parameter = new SqlParameter("@User_Status", SqlDbType.Char, 1);
            parameter.Value = status;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status,User_Create_Date,User_Update_Date FROM Sys_User where IsDel='0' and User_Status=@User_Status", new SqlParameter[] { parameter }))
            {
                while (reader.Read())
                {
                    UserInfo info = new UserInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4));
                    list.Add(info);
                }
            }
            return list;
        }

        public IList getUserList(string user_name, string user_logon_code)
        {
            IList list = new ArrayList();
            string cmdText = string.Format("SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status,User_Create_Date,User_Update_Date FROM Sys_User where IsDel='0' and User_Logon_Code like '%{0}%' and User_Name like '%{1}%'", user_logon_code, user_name);
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, null))
            {
                while (reader.Read())
                {
                    UserInfo info = new UserInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4));
                    list.Add(info);
                }
            }
            return list;
        }

        public IList getUserListByCode(string user_logon_code)
        {
            IList list = new ArrayList();
            string cmdText = string.Format("SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status,User_Create_Date,User_Update_Date FROM Sys_User where IsDel='0' and User_Logon_Code like '%{0}%'", user_logon_code);
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, null))
            {
                while (reader.Read())
                {
                    UserInfo info = new UserInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4));
                    list.Add(info);
                }
            }
            return list;
        }

        public IList getUserListByName(string user_name)
        {
            IList list = new ArrayList();
            string cmdText = string.Format("SELECT User_UID,User_Name,User_Logon_Code,User_Password,User_Status,User_Create_Date,User_Update_Date FROM Sys_User where IsDel='0' and User_Name like '%{0}%'", user_name);
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, null))
            {
                while (reader.Read())
                {
                    UserInfo info = new UserInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4));
                    list.Add(info);
                }
            }
            return list;
        }

        private static SqlParameter[] GetUserModifyParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("UPDATE Sys_User SET User_Name = @User_Name, User_Logon_Code = @User_Logon_Code, User_Password = @User_Password, User_Status = @User_Status, User_Update_Date = getDate() WHERE User_UID = @User_UID");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@User_UID", SqlDbType.Int, 4), new SqlParameter("@User_Name", SqlDbType.VarChar, 0x20), new SqlParameter("@User_Logon_Code", SqlDbType.VarChar, 0x26), new SqlParameter("@User_Password", SqlDbType.VarChar, 0x80), new SqlParameter("@User_Status", SqlDbType.Char, 1) };
                SQLHelper.CacheParameters("UPDATE Sys_User SET User_Name = @User_Name, User_Logon_Code = @User_Logon_Code, User_Password = @User_Password, User_Status = @User_Status, User_Update_Date = getDate() WHERE User_UID = @User_UID", cachedParameters);
            }
            return cachedParameters;
        }

        public void Insert(UserInfo acc)
        {
            SqlParameter[] userInsertParameters = GetUserInsertParameters();
            this.SetUserInsertParameters(userInsertParameters, acc);
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "INSERT INTO Sys_User VALUES(@User_Name,@User_Logon_Code,@User_Password,@User_Status,getDate(),null,'0')", userInsertParameters);
                        transaction.Commit();
                    }
                    catch (SqlException exception)
                    {
                        transaction.Rollback();
                        Console.WriteLine(exception.Message);
                    }
                }
            }
        }

        private void SetUserInsertParameters(SqlParameter[] parms, UserInfo acc)
        {
            parms[0].Value = acc.User_Name;
            parms[1].Value = acc.User_Logon_Code;
            parms[2].Value = acc.User_Password;
            parms[3].Value = acc.User_Status;
        }

        private void SetUserModifyParameters(SqlParameter[] parms, UserInfo acc)
        {
            parms[0].Value = acc.User_UID;
            parms[1].Value = acc.User_Name;
            parms[2].Value = acc.User_Logon_Code;
            parms[3].Value = acc.User_Password;
            parms[4].Value = acc.User_Status;
        }

        public void Update(UserInfo myItem)
        {
            SqlParameter[] userModifyParameters = GetUserModifyParameters();
            this.SetUserModifyParameters(userModifyParameters, myItem);
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "UPDATE Sys_User SET User_Name = @User_Name, User_Logon_Code = @User_Logon_Code, User_Password = @User_Password, User_Status = @User_Status, User_Update_Date = getDate() WHERE User_UID = @User_UID", userModifyParameters);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
    }
}
